package com.linck.port.listener;

import com.linck.port.constant.Constans;
import com.linck.port.util.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * @program: AirPort
 * @description Redis过期时间监听
 * @author: Linck
 * @create: 2020-06-11 18:08
 **/
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    private static final Logger logger = LoggerFactory.getLogger(RedisKeyExpirationListener.class);

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String expireKey = message.toString();
        if(expireKey.startsWith(Constans.PORT_PREFIX)){
            File filePath = new File("/PortFile/"+expireKey+"/");
            FileUtil.deleteFile(filePath);
            logger.info("过期key："+expireKey+"-对于文件以删除");
        }
    }
}
